package com.zxbit.flash.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.zxbit.flash.R;
import com.zxbit.flash.utils.RandomCodeUtils;
import com.zxbit.flash.utils.StringUtils;

public class RandomCodeDialog extends Dialog implements View.OnClickListener {

    private View rootView;
    private EditText edit_random_code;
    private ImageView iv_showCode;
    private TextView confirm_button;
    private RandomClickListener mRandomClickListener;
    //产生的验证码
    private String realCode;


    public RandomCodeDialog(@NonNull Context context) {
        super(context);
        init(context);
    }

    public RandomCodeDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    protected RandomCodeDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    private void init(Context context) {
        rootView = LayoutInflater.from(context).inflate(R.layout.layout_random_code_dialog, null);
        edit_random_code = rootView.findViewById(R.id.edit_random_code);
        iv_showCode = rootView.findViewById(R.id.iv_showCode);
        confirm_button = rootView.findViewById(R.id.confirm_button);
        setContentView(rootView);

        Window window = getWindow();
        assert window != null;
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.CENTER;
        // 设置对话框大小
        params.width = context.getResources().getDimensionPixelOffset(R.dimen.dialog_width);
        window.setAttributes(params);

        //将验证码用图片的形式显示出来
        iv_showCode.setImageBitmap(RandomCodeUtils.getInstance().createBitmap());
        realCode = RandomCodeUtils.getInstance().getCode().toLowerCase();
        iv_showCode.setOnClickListener(this);

        confirm_button.setOnClickListener(this);
        setCanceledOnTouchOutside(true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.confirm_button:
                if (mRandomClickListener != null) {
                    mRandomClickListener.onConfirmClick();
                }
                break;
            case R.id.iv_showCode:
                rebuildCode();
                break;
        }
    }

    public String getRealCode() {
        return realCode;
    }

    public String getEditCode() {
        return edit_random_code.getText().toString();
    }

    public void rebuildCode() {
        edit_random_code.setText("");
        iv_showCode.setImageBitmap(RandomCodeUtils.getInstance().createBitmap());
        realCode = RandomCodeUtils.getInstance().getCode();
    }

    public RandomCodeDialog setRandomClickListener(RandomClickListener listener) {
        mRandomClickListener = listener;
        return this;
    }

    @Override
    public void dismiss() {
        rebuildCode();
        if (edit_random_code != null && !StringUtils.isEmpty(edit_random_code.getText().toString())) {
            edit_random_code.setText("");
        }
        super.dismiss();
    }

    public interface RandomClickListener {
        void onConfirmClick();
    }
}
